using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZKLT25.API.Helper;
using ZKLT25.API.IServices;
using ZKLT25.API.IServices.Dtos;

namespace ZKLT25.API.Controllers
{
    /// <summary>
    /// 阀体型号维护控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class Ask_FTListController : ControllerBase
    {
        private readonly IAsk_FTListService _service;

        public Ask_FTListController(IAsk_FTListService service)
        {
            _service = service;
        }

        /// <summary>
        /// 分页查询阀体型号列表
        /// </summary>
        /// <param name="qto">查询条件</param>
        /// <returns></returns>
        [HttpGet("GetPagedList")]
        public async Task<ResultModel<PaginationList<Ask_FTListDto>>> GetPagedListAsync([FromQuery] Ask_FTListQto qto)
        {
            var res = await _service.GetPagedListAsync(qto);
            if (res.Data != null)
                Response.Headers.Append("TotalCount", res.Data.TotalCount.ToString());
            return res;
        }

        /// <summary>
        /// 创建阀体型号
        /// </summary>
        /// <param name="cto">创建请求</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> CreateAsync([FromBody] Ask_FTListCto cto)
        {
            return await _service.CreateAsync(cto);
        }

        /// <summary>
        /// 更新阀体型号
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="uto">更新请求</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResultModel<bool>> UpdateAsync([FromRoute] int id, [FromBody] Ask_FTListUto uto)
        {
            return await _service.UpdateAsync(id, uto);
        }

        /// <summary>
        /// 删除阀体型号
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResultModel<bool>> DeleteAsync([FromRoute] int id)
        {
            return await _service.DeleteAsync(id);
        }

        /// <summary>
        /// 检查阀体型号是否存在
        /// </summary>
        /// <param name="ftVersion">阀体型号</param>
        /// <param name="excludeId">排除的ID</param>
        /// <returns></returns>
        [HttpGet("CheckExists")]
        public async Task<ResultModel<bool>> CheckExistsAsync([FromQuery] string ftVersion, [FromQuery] int? excludeId = null)
        {
            return await _service.ExistsAsync(ftVersion, excludeId);
        }

        /// <summary>
        /// 批量更新系数
        /// </summary>
        [HttpPost("BatchUpdateRatio")]
        public async Task<ResultModel<bool>> BatchUpdateRatioAsync([FromBody] BatchUpdateRatioRequest request)
        {
            return await _service.BatchUpdateRatioAsync(request.Ids, request.Ratio);
        }
    }

    /// <summary>
    /// 附件控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class Ask_FJListController : ControllerBase
    {
        private readonly IAsk_FJListService _service;

        public Ask_FJListController(IAsk_FJListService service)
        {
            _service = service;
        }

        /// <summary>
        /// 分页查询附件列表
        /// </summary>
        /// <param name="qto">查询条件</param>
        /// <returns></returns>
        [HttpGet("GetPagedList")]
        public async Task<ResultModel<PaginationList<Ask_FJListDto>>> GetPagedListAsync([FromQuery] Ask_FJListQto qto)
        {
            var res = await _service.GetPagedListAsync(qto);
            if (res.Data != null)
                Response.Headers.Append("TotalCount", res.Data.TotalCount.ToString());
            return res;
        }

        /// <summary>
        /// 批量更新系数
        /// </summary>
        [HttpPost("BatchUpdateRatio")]
        public async Task<ResultModel<bool>> BatchUpdateRatioAsync([FromBody] BatchUpdateRatioRequest request)
        {
            return await _service.BatchUpdateRatioAsync(request.Ids, request.Ratio);
        }
    }

    /// <summary>
    /// 阀体 / 附件日志控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class Ask_FTFJListLogController : ControllerBase
    {
        private readonly IAsk_FTFJListLogService _service;

        public Ask_FTFJListLogController(IAsk_FTFJListLogService service)
        {
            _service = service;
        }

        /// <summary>
        /// 分页查询阀体 / 附件日志
        /// </summary>
        /// <param name="qto">查询条件（MainID 和 DataType 必传）</param>
        /// <returns>分页日志列表</returns>
        [HttpGet("GetPagedList")]
        public async Task<ResultModel<PaginationList<Ask_FTFJListLogDto>>> GetPagedListAsync([FromQuery] Ask_FTFJListLogQto qto)
        {
            var res = await _service.GetPagedListAsync(qto);
            if (res.Data != null)
                Response.Headers.Append("TotalCount", res.Data.TotalCount.ToString());
            return res;
        }
    }
}
